/*
    This file is part of Emu.

    Emu is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Emu is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Emu.  If not, see <http://www.gnu.org/licenses/>. */
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QMessageBox>

#include "ealiaseditor.h"

EAliasEditor::EAliasEditor(QWidget *parent, bool addmode)
    : QDialog(parent)
{
    QFormLayout *layout = new QFormLayout;
    QDialogButtonBox *box = new QDialogButtonBox(QDialogButtonBox::Ok |
                                                 QDialogButtonBox::Cancel);

    _nameLine = new QLineEdit;
    _inputLine = new QLineEdit;
    _outputLine = new QLineEdit;

    layout->addRow(tr("Name:"), _nameLine);
    layout->addRow(tr("Input:"), _inputLine);
    layout->addRow(tr("Output:"), _outputLine);
    layout->addWidget(box);
    setLayout(layout);

    if (addmode)
        setWindowTitle(tr("Add Alias"));
    else
        setWindowTitle(tr("Edit Alias"));

    connect(box, SIGNAL(rejected()), this, SLOT(reject()));
    connect(box, SIGNAL(accepted()), this, SLOT(validate()));
}

void EAliasEditor::useAlias(EAlias alias)
{
    _nameLine->setText(alias.name());
    _inputLine->setText(alias.input());
    _outputLine->setText(alias.output());
}

void EAliasEditor::validate()
{
    if (_inputLine->text().isEmpty()) {
        QMessageBox::critical(this, tr("Error"), tr("Alias has no input."));
        return;
    }

    _alias.setName(_nameLine->text());
    _alias.setInput(_inputLine->text());
    _alias.setOutput(_outputLine->text());

    accept();
}
